package tron

import (
	"github.com/fbsobreira/gotron-sdk/pkg/common"
)

func TransferTrx(pr string, from string, to string, amount float64) (string, error) {
	//c, err := NewClient("54.168.218.95:50051")
	c, err := NewClient(GetTrcApiRpcUrl())
	if err != nil {
		return "", err
	}
	defer c.Close()
	txNumber := TRX.ToSun(amount).Int64()
	tx, err := c.Transfer(from, to, txNumber)
	if err != nil {
		return "", err
	}
	signTx, err := SignTransaction(tx.Transaction, pr)
	if err != nil {
		return "", err
	}
	err = c.BroadcastTransaction(signTx)
	if err != nil {
		return "", err
	}
	return common.BytesToHexString(tx.GetTxid()), nil
}

func TransferTrc20(pr string, from string, to string, contract string, amount float64) (string, error) {
	c, err := NewClient(GetTrcApiRpcUrl())
	if err != nil {
		return "", err
	}
	defer c.Close()

	txNumber := TRX.ToSun(amount)
	feeLimitSun := int64(1_000_000_000)
	tx, err := c.TransferTrc20(from, to, contract, txNumber, feeLimitSun)
	if err != nil {
		return "", err
	}
	signTx, err := SignTransaction(tx.Transaction, pr)
	if err != nil {
		return "", err
	}
	err = c.BroadcastTransaction(signTx)
	if err != nil {
		return "", err
	}
	return common.BytesToHexString(tx.GetTxid()), nil
}
